﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBA.Common;
using System.IO;
using System.Data;
using DBA.Model;
using DBA.Data;
using DBA.Data.ADO;

namespace DBA.Web.AwardManage
{
    public partial class AwardAdd : System.Web.UI.Page
    {
        public string attachHtml = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!String.IsNullOrEmpty(Request.Params["id"]))
                {
                    hidID.Value = RequestHelper.QueryString("id");
                    hidType.Value = Request.Params["type"];
                    ModifyStudentInit();
                }
            }
        }

        private void ModifyStudentInit()
        {
            string id = hidID.Value;
            if (!VerificationHelper.IsNullOrEmpty<string>(id))
            {
                Awards model = dal.ModifyData.GetEntity<Awards>(id);
                if (!VerificationHelper.IsNullOrEmpty<Awards>(model)) //如果学生对象不为空，那么对控件赋值
                {
                    ConvertHelper.ModelToControl<Awards>(model, pan_tab.Controls);                   
                }


                //txtspPerson.Value = GetUserName(model.Sp_Person);
            }

            GetAttach();


        }

        protected void GetAttach()
        {
            string id = hidID.Value;

            if (!VerificationHelper.IsNullOrEmpty<string>(id))
            {
                string strSql = "Select * from PorjectAttachment where aw_ID=@aw_ID ";
                dal.ManagerParameter.ClearParameter();
                dal.ManagerParameter.AddNewInParameter("aw_ID", id);
                DataTable dt = dal.ExecuteFillDataTable(strSql);
                foreach (DataRow dr in dt.Rows)
                {
                    attachHtml += "<div id='div" + dr["Paid"].ToString() + "'><a href='javascript:void(0)' title='点击下载附件'><span onclick='downFile(" + dr["Paid"].ToString() + ")'>" + dr["PaName"].ToString() + "</span></a>&nbsp;&nbsp; &nbsp; <a href='javascript:void(0)' onclick='deleteAtt(" + dr["Paid"].ToString() + ")'>删除</a></br></div>";
                }

            }
        }

        DataAccessLibrary dal = CommonQuery.DataAccessLibrary;

        protected void lnkSave_Click(object sender, EventArgs e)
        {
            if (ddl_Person1Is.SelectedValue != "-2")
            {
                if (ddl_Person1Is.SelectedValue == "1")
                {
                    if (!IsExsistEmp(txt_aw_Person1.Text.Trim()))
                    {
                        ScriptManager.RegisterStartupScript(this, base.GetType(), "insertOk" + System.Guid.NewGuid().ToString(), "alert('第一作者错误，教师中没有此人，请检查!');", true);
                        return;
                    }
                }

                if (ddl_Person1Is.SelectedValue == "2")
                {
                    if (!IsExsistStu(txt_aw_Person1.Text.Trim()))
                    {
                        ScriptManager.RegisterStartupScript(this, base.GetType(), "insertOk" + System.Guid.NewGuid().ToString(), "alert('第一作者错误，学生中没有此人，请检查!');", true);
                        return;
                    }
                }
            }

            if (ddl_Person2Is.SelectedValue != "-2")
            {
                if (ddl_Person2Is.SelectedValue == "1")
                {
                    if (!IsExsistEmp(txt_aw_Person2.Text.Trim()))
                    {
                        ScriptManager.RegisterStartupScript(this, base.GetType(), "insertOk" + System.Guid.NewGuid().ToString(), "alert('第二作者错误，教师中没有此人，请检查!');", true);
                        return;
                    }
                }

                if (ddl_Person2Is.SelectedValue == "2")
                {
                    if (!IsExsistStu(txt_aw_Person2.Text.Trim()))
                    {
                        ScriptManager.RegisterStartupScript(this, base.GetType(), "insertOk" + System.Guid.NewGuid().ToString(), "alert('第二作者错误，学生中没有此人，请检查!');", true);
                        return;
                    }
                }
            }

            if (ddl_Person3Is.SelectedValue != "-2")
            {
                if (ddl_Person3Is.SelectedValue == "1")
                {
                    if (!IsExsistEmp(txt_aw_Person3.Text.Trim()))
                    {
                        ScriptManager.RegisterStartupScript(this, base.GetType(), "insertOk" + System.Guid.NewGuid().ToString(), "alert('第三作者错误，教师中没有此人，请检查!');", true);
                        return;
                    }
                }

                if (ddl_Person3Is.SelectedValue == "2")
                {
                    if (!IsExsistStu(txt_aw_Person3.Text.Trim()))
                    {
                        ScriptManager.RegisterStartupScript(this, base.GetType(), "insertOk" + System.Guid.NewGuid().ToString(), "alert('第三作者错误，学生中没有此人，请检查!');", true);
                        return;
                    }
                }
            }
            if (ddl_Person4Is.SelectedValue != "-2")
            {
                if (ddl_Person4Is.SelectedValue == "1")
                {
                    if (!IsExsistEmp(txt_aw_Person4.Text.Trim()))
                    {
                        ScriptManager.RegisterStartupScript(this, base.GetType(), "insertOk" + System.Guid.NewGuid().ToString(), "alert('第四作者错误，教师中没有此人，请检查!');", true);
                        return;
                    }
                }

                if (ddl_Person4Is.SelectedValue == "2")
                {
                    if (!IsExsistStu(txt_aw_Person4.Text.Trim()))
                    {
                        ScriptManager.RegisterStartupScript(this, base.GetType(), "insertOk" + System.Guid.NewGuid().ToString(), "alert('第四作者错误，学生中没有此人，请检查!');", true);
                        return;
                    }
                }
            }

            if (ddl_Person5Is.SelectedValue != "-2")
            {
                if (ddl_Person5Is.SelectedValue == "1")
                {
                    if (!IsExsistEmp(txt_aw_Person5.Text.Trim()))
                    {
                        ScriptManager.RegisterStartupScript(this, base.GetType(), "insertOk" + System.Guid.NewGuid().ToString(), "alert('第五作者错误，教师中没有此人，请检查!');", true);
                        return;
                    }
                }

                if (ddl_Person5Is.SelectedValue == "2")
                {
                    if (!IsExsistStu(txt_aw_Person5.Text.Trim()))
                    {
                        ScriptManager.RegisterStartupScript(this, base.GetType(), "insertOk" + System.Guid.NewGuid().ToString(), "alert('第五作者错误，学生中没有此人，请检查!');", true);
                        return;
                    }
                }
            }

            if (ddl_Person6Is.SelectedValue != "-2")
            {
                if (ddl_Person6Is.SelectedValue == "1")
                {
                    if (!IsExsistEmp(txt_aw_Person6.Text.Trim()))
                    {
                        ScriptManager.RegisterStartupScript(this, base.GetType(), "insertOk" + System.Guid.NewGuid().ToString(), "alert('第六作者错误，教师中没有此人，请检查!');", true);
                        return;
                    }
                }

                if (ddl_Person6Is.SelectedValue == "2")
                {
                    if (!IsExsistStu(txt_aw_Person6.Text.Trim()))
                    {
                        ScriptManager.RegisterStartupScript(this, base.GetType(), "insertOk" + System.Guid.NewGuid().ToString(), "alert('第六作者错误，学生中没有此人，请检查!');", true);
                        return;
                    }
                }
            }

            if (ddl_Person7Is.SelectedValue != "-2")
            {
                if (ddl_Person7Is.SelectedValue == "1")
                {
                    if (!IsExsistEmp(txt_aw_Person7.Text.Trim()))
                    {
                        ScriptManager.RegisterStartupScript(this, base.GetType(), "insertOk" + System.Guid.NewGuid().ToString(), "alert('第七作者错误，教师中没有此人，请检查!');", true);
                        return;
                    }
                }

                if (ddl_Person7Is.SelectedValue == "2")
                {
                    if (!IsExsistStu(txt_aw_Person7.Text.Trim()))
                    {
                        ScriptManager.RegisterStartupScript(this, base.GetType(), "insertOk" + System.Guid.NewGuid().ToString(), "alert('第七作者错误，学生中没有此人，请检查!');", true);
                        return;
                    }
                }
            }

           

            Awards model = ConvertHelper.ControlToModel<Awards>(pan_tab.Controls);           
            //model.Sp_Person = hidSelectUser.Value;
            if (hidID.Value.Equals(string.Empty))//添加学生信息
            {
                System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
                string filePath = "/upload/awards/";
                string absolutPath = MapPath(filePath);
                if (!Directory.Exists(absolutPath))
                {
                    Directory.CreateDirectory(absolutPath);
                }
                hdResult.Value = dal.ModifyData.InsertGetID<Awards>(model).ToString();
                for (int i = 0; i < files.Count; i++)
                {
                    if (files[i].FileName != "")
                    {
                        string fileCode = DateTime.Now.ToString("yyyyMMddHHmmssfff") + i.ToString() + files[i].FileName.Substring(files[i].FileName.LastIndexOf('.'));
                        hidFileName.Value += "," + files[i].FileName;
                        files[i].SaveAs(absolutPath + fileCode);

                        //插入附件
                        PorjectAttachment att = new PorjectAttachment();
                        att.Pap_ps_ID = int.Parse(hdResult.Value);
                        att.PaName = files[i].FileName;
                        att.PaCode = fileCode;
                        dal.ModifyData.Insert<PorjectAttachment>(att);
                    }
                }
                ScriptManager.RegisterStartupScript(this, base.GetType(), "insertOk" + System.Guid.NewGuid().ToString(), "Public.OpertAlert('操作成功!',true, 'AspNetPager1');", true);

            }
            else //修改学生信息
            {
                model.Aw_ID = ConvertHelper.ToInt32(hidID.Value);
                if (dal.ModifyData.Update<Awards>(model) == "0")
                {
                    //hdResult.Value = "0";
                    ScriptManager.RegisterStartupScript(this, base.GetType(), "insertOk" + System.Guid.NewGuid().ToString(), "alert('操作失败!');", true);
                }
                else
                {
                    string value = hidAttach.Value.TrimStart(',');
                    string sql = "delete from [PorjectAttachment] where Paid in (" + value + ");";
                    dal.ExecuteNonQuery(sql);


                    //hdResult.Value = "1";
                    System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
                    string filePath = "/upload/awards/";
                    string absolutPath = MapPath(filePath);
                    if (!Directory.Exists(absolutPath))
                    {
                        Directory.CreateDirectory(absolutPath);
                    }

                    for (int i = 0; i < files.Count; i++)
                    {
                        if (files[i].FileName != "")
                        {
                            string fileCode = DateTime.Now.ToString("yyyyMMddHHmmssfff") + i.ToString() + files[i].FileName.Substring(files[i].FileName.LastIndexOf('.'));
                            hidFileName.Value += "," + files[i].FileName;
                            files[i].SaveAs(absolutPath + fileCode);

                            //插入附件
                            PorjectAttachment att = new PorjectAttachment();
                            att.Aw_ID = model.Aw_ID;
                            att.PaName = files[i].FileName;
                            att.PaCode = fileCode;
                            dal.ModifyData.Insert<PorjectAttachment>(att);
                        }
                    }

                    ScriptManager.RegisterStartupScript(this, base.GetType(), "insertOk" + System.Guid.NewGuid().ToString(), "Public.OpertAlert('操作成功!',true, 'AspNetPager1');", true);
                }
            }
        }

        protected bool IsExsistEmp(string Name)
        {
            string strSql = "select EmployeeID from EmployeeInfo where Name=@Name ";
            dal.ManagerParameter.ClearParameter();
            dal.ManagerParameter.AddNewInParameter("Name", Name);

            DataTable dt = dal.ExecuteFillDataTable(strSql);
            bool flag = false;
            if (dt.Rows.Count == 1)
            {
                flag = true;
            }

            return flag;
        }

        protected bool IsExsistStu(string Name)
        {
            string strSql = "select id from Student where Name=@Name ";
            dal.ManagerParameter.ClearParameter();
            dal.ManagerParameter.AddNewInParameter("Name", Name);

            DataTable dt = dal.ExecuteFillDataTable(strSql);
            bool flag = false;
            if (dt.Rows.Count == 1)
            {
                flag = true;
            }

            return flag;
        }
    }
}